package hr.ctg.domain.action.korisnik;

import hr.ctg.domain.model.DojamPosjetiteljaPortala;
import hr.ctg.domain.model.Korisnik;
import hr.ctg.domain.model.Objekt;
import hr.ctg.domain.model.Osobina;
import hr.ctg.service.dao.MjestoDAO;
import hr.ctg.service.dao.ObjektDAO;
import hr.ctg.service.dao.OtokDAO;
import hr.ctg.service.dao.ParkDAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletRequest;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.faces.Renderer;
import org.jboss.seam.log.Log;

@Name(value = "feedbackAction")
public class FeedbackAction {
	@Logger
	Log log;

	@In(required = false)
	EntityManager entityManager;
	@In
	private Renderer renderer;
	@In
	private Map<String, String> messages;
	@In(required = false)
	FacesMessages facesMessages;
	@In
	ObjektDAO objektDAO;
	@In
	MjestoDAO mjestoDAO;
	@In
	OtokDAO otokDAO;
	@In
	ParkDAO parkDAO;

	@In(required = false)
	Korisnik korisnik;

	@In(required = false)
	Map<String, List<String>> comparisonTable;

	@RequestParameter
	Integer idObjekta;
	@RequestParameter
	Integer idMjesta;
	@RequestParameter
	Integer idOtoka;
	@RequestParameter
	Integer idParka;

	private String feedbackMessage;
	private String inaccurateMessage;
	private String feedbackFriendEmail;
	private String feedbackEmail;
	private String feedbackName;
	private String inNaziv;
	private Integer inId;
	Integer feedbackAdultAgeCnt;
	Integer feedbackChildAgeCnt;
	Date feedbackDateArrival;
	Date feedbackDateDeparture;
	String feedbackType;
	List<Osobina> osobine = new ArrayList<Osobina>();
	Objekt objekt;

	public void registerFeedback() {
		// String outcome = null;

		log.debug("Registriram feedback... " + feedbackType);
		final DojamPosjetiteljaPortala dojamPosjetiteljaPortala = new DojamPosjetiteljaPortala();
		log.debug("VRIJEDNOST IZABRANA " + feedbackType);

		try {
			if ("i".equals(feedbackType) || "ri".equals(feedbackType)) {
				try {
					renderer.render("/templates/mail/contactObjekt.xhtml");
				} catch (final Exception e) {
					log.warn("IMAMO PROBLEM SA SERVEROM - OFFLINE", e);
				}
				// outcome = "emailSent";
			} else if ("o".equals(feedbackType)) {
				try {
					log.error("Objekt id> " + idObjekta);
					objekt = entityManager.find(Objekt.class, idObjekta);

					// for (final Osobina o : objekt.getOsobinas()) {
					// o.getNaziv();
					// }
					osobine.addAll(objekt.getOsobinas());

					renderer.render("/templates/mail/showObjekt.xhtml");
					log.debug("Poslao podatke o objektu prijatelju...");
				} catch (final Exception e) {
					log.warn("IMAMO PROBLEM SA SERVEROM - OFFLINE", e);
					facesMessages.addFromResourceBundle(
							FacesMessage.SEVERITY_ERROR,
							"message.email.sent.unsuccessfull");
					return;
				}

				// outcome = "emailSent";
			} else {
				if ("misljenje".equals(feedbackType)) {
					dojamPosjetiteljaPortala.setDatum(new Date());
					dojamPosjetiteljaPortala.setKorisnik(korisnik);
					dojamPosjetiteljaPortala.setKomentar(feedbackMessage);
					entityManager.persist(dojamPosjetiteljaPortala);
				}
				renderer.render("/templates/mail/feedback.xhtml");
				// outcome = "emailSent";
			}

			feedbackMessage = null;
			feedbackFriendEmail = null;
			feedbackEmail = null;
			feedbackName = null;
			feedbackAdultAgeCnt = null;
			feedbackChildAgeCnt = null;
			feedbackDateArrival = null;
			feedbackDateDeparture = null;
		} catch (final NoResultException e) {
			log.error(e.getMessage(), e);
			facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,
					"message.email.sent.unsuccessfull");
			return;
		}

		facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
				"message.email.sent.successfull");
		// return outcome;
	}

	public void sendComparison() {
		log.debug("Saljem usporedbu...");
		try {
			if (korisnik == null) {
			} else {
			}
			try {
				renderer.render("/templates/mail/comparedObjekt.xhtml");
				facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
						"message.email.sent.successfull");
			} catch (final Exception e) {
				log.error("IMAMO PROBLEM SA SERVEROM - OFFLINE", e);
				facesMessages.addFromResourceBundle(
						FacesMessage.SEVERITY_ERROR,
						"message.email.sent.unsuccessfull");
			}
		} catch (final NoResultException e) {
			log.debug(e.getMessage(), e);
		}
		// return "emailSent";
	}

	public void sendInaccurateData() {
		try {
			renderer.render("/templates/mail/inaccurate.xhtml");
			facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
					"message.email.sent.successfull");
		} catch (final Exception e) {
			log.error("IMAMO PROBLEM SA SERVEROM - OFFLINE", e);
			facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,
					"message.email.sent.unsuccessfull");
		}
	}

	public String getFeedbackMessage() {
		return feedbackMessage;
	}

	public void setFeedbackMessage(final String feedbackMessage) {
		this.feedbackMessage = feedbackMessage;

	}

	public String getFeedbackEmail() {
		return feedbackEmail;
	}

	public String getFeedbackName() {
		return feedbackName;
	}

	public void setFeedbackEmail(final String feedbackEmail) {
		this.feedbackEmail = feedbackEmail;

	}

	public void setFeedbackName(final String feedbackName) {
		this.feedbackName = feedbackName;

	}

	public Integer getFeedbackAdultAgeCnt() {
		return feedbackAdultAgeCnt;
	}

	public void setFeedbackAdultAgeCnt(final Integer feedbackAdultAgeCnt) {
		this.feedbackAdultAgeCnt = feedbackAdultAgeCnt;
	}

	public Integer getFeedbackChildAgeCnt() {
		return feedbackChildAgeCnt;
	}

	public void setFeedbackChildAgeCnt(final Integer feedbackChildAgeCnt) {
		this.feedbackChildAgeCnt = feedbackChildAgeCnt;
	}

	public Date getFeedbackDateArrival() {
		return feedbackDateArrival;
	}

	public void setFeedbackDateArrival(final Date feedbackDateArrival) {
		this.feedbackDateArrival = feedbackDateArrival;
	}

	public Date getFeedbackDateDeparture() {
		return feedbackDateDeparture;
	}

	public void setFeedbackDateDeparture(final Date feedbackDateDeparture) {
		this.feedbackDateDeparture = feedbackDateDeparture;
	}

	public String getFeedbackFriendEmail() {
		return feedbackFriendEmail;
	}

	public void setFeedbackFriendEmail(final String feedbackFriendEmail) {
		this.feedbackFriendEmail = feedbackFriendEmail;
	}

	public void checkEmail(final FacesContext facesContext,
			final UIComponent uiComponent, final Object object) {
		// skupi s interneta neki bolji izraz za validaciju emaila...
		final String rule = ".*@.*\\..*";
		final String email = (String) object;
		log.debug("ispitujem: " + email);
		if (!email.matches(rule)) {
			throw new hr.ctg.domain.action.exception.ValidatorException(
					new FacesMessage(messages
							.get("message.email.invalidformat")));
		}
	}

	public List<Osobina> getOsobine() {
		return osobine;
	}

	public void setOsobine(final List<Osobina> osobine) {
		this.osobine = osobine;
	}

	public Integer getIdMjesta() {
		return idMjesta;
	}

	public void setIdMjesta(final Integer idMjesta) {
		this.idMjesta = idMjesta;
	}

	public String getFeedbackType() {
		return feedbackType;
	}

	public void setFeedbackType(final String feedbackType) {
		this.feedbackType = feedbackType;
	}

	public String getInNaziv() {
		return inNaziv;
	}

	public void setInNaziv(final String inNaziv) {
		this.inNaziv = inNaziv;
	}

	public Integer getInId() {
		return inId;
	}

	public void setInId(final Integer inId) {
		this.inId = inId;
	}

	public Objekt getObjekt() {
		return objekt;
	}

	public void setObjekt(Objekt objekt) {
		this.objekt = objekt;
	}

	public List<String> getOsobineUUsporedbi() {
		List<String> l = new ArrayList<String>();
		if (null != comparisonTable)
			l.addAll(comparisonTable.keySet());
		return l;
	}

	public String getInaccurateMessage() {
		if (inaccurateMessage == null) {
			inaccurateMessage = "URL: ";
			inaccurateMessage += ((HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest())
					.getRequestURL().toString()
					+ "?"
					+ ((HttpServletRequest) FacesContext.getCurrentInstance()
							.getExternalContext().getRequest())
							.getQueryString();
			inaccurateMessage += "\n\n";
		}
		return inaccurateMessage;
	}

	public void setInaccurateMessage(String inaccurateMessage) {
		this.inaccurateMessage = inaccurateMessage;
	}
}
